package com.ukao.steward.dialog;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseDialogFragment;
import com.ukao.steward.listener.DialogSubmitListener;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.widget.StateButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.steward.R.id.look_btn;
import static com.ukao.steward.consts.Constant.ARG_PARAM3;
import static com.ukao.steward.consts.Constant.ARG_PARAM4;

/**
 * 接单提示
 */
public class OrderReceivDialogFragment extends BaseDialogFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.close_btn)
    ImageView closeBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.image_state)
    ImageView imageState;
    @BindView(R.id.text_state)
    TextView textState;
    @BindView(look_btn)
    StateButton lookBtn;
    Unbinder unbinder;
    @BindView(R.id.trans_no_layout)
    RelativeLayout transNoLayout;
    @BindView(R.id.look_line)
    View lookLine;
    private String errorMsg;
    private boolean isReceivingSucceed;
    private String dialogTitele;
    private String orderId;


    public static OrderReceivDialogFragment newInstance(String dialogTitele, boolean isReceivingSucceed, String errorMsg, String orderId) {
        OrderReceivDialogFragment fragment = new OrderReceivDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, dialogTitele);
        args.putBoolean(ARG_PARAM2, isReceivingSucceed);
        args.putString(ARG_PARAM3, errorMsg);
        args.putString(ARG_PARAM4, orderId);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            dialogTitele = getArguments().getString(ARG_PARAM1);
            isReceivingSucceed = getArguments().getBoolean(ARG_PARAM2, false);
            errorMsg = getArguments().getString(ARG_PARAM3);
            orderId = getArguments().getString(ARG_PARAM4);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_order_receiv_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        setShowBottom(true);
        setIsWithWrap(false);
        title.setText(dialogTitele);
        if (!isReceivingSucceed) {
            textState.setText(CheckUtils.isEmptyString(errorMsg));
            imageState.setImageResource(R.drawable.batch_warning);
            lookLine.setVisibility(View.INVISIBLE);
            lookBtn.setVisibility(View.INVISIBLE);
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.close_btn, look_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();
                break;
            case look_btn:

                if (mDialogSubmitListener != null) {
                    mDialogSubmitListener.onSubmitClick(look_btn, orderId);
                }
                dismiss();
                break;
        }
    }


    public static Builder createBuilder(
            FragmentManager fragmentManager) {
        return new Builder(fragmentManager);
    }

    public static final class Builder {
        private final FragmentManager mFragmentManager;
        private DialogSubmitListener mDialogSubmitListener;
        private String dialogTitele = "提示";
        private boolean isReceivingSucceed;
        private String errorMsg;
        private String orderId;

        public Builder(FragmentManager fragmentManager) {
            mFragmentManager = fragmentManager;
        }

        public Builder mDialogSubmitListener(DialogSubmitListener val) {
            mDialogSubmitListener = val;
            return this;
        }

        public Builder dialogTitele(String val) {
            dialogTitele = val;
            return this;
        }

        public Builder isReceivingSucceed(boolean val) {
            isReceivingSucceed = val;
            return this;
        }

        public Builder orderId(String val) {
            orderId = val;
            return this;
        }

        public Builder errorMsg(String val) {
            errorMsg = val;
            return this;
        }

        public OrderReceivDialogFragment show() {
            OrderReceivDialogFragment mOrderReceivDialog = OrderReceivDialogFragment.newInstance(dialogTitele, isReceivingSucceed, errorMsg, orderId);
            mOrderReceivDialog.setOnSubmitClickListener(mDialogSubmitListener);
            mOrderReceivDialog.show(mFragmentManager, mOrderReceivDialog.getClass().getName());
            return mOrderReceivDialog;
        }
    }
}
